package org.liveboardgames.agricola.control.commands
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    
    import mx.collections.ArrayCollection;
    import mx.collections.ListCollectionView;
    import mx.controls.Alert;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    
    import org.liveboardgames.agricola.control.delegates.BuildImprovementDelegate;
    import org.liveboardgames.agricola.control.events.BuildImprovementEvent;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.vo.player.Player;
    
    public class BuildImprovementCommand implements ICommand, IResponder
    {
    	private var player:Player;
    	
        public function execute(event:CairngormEvent):void
        {
            var delegate:BuildImprovementDelegate = new BuildImprovementDelegate(this);
            var buildImprovementEvent:BuildImprovementEvent = BuildImprovementEvent(event);
            this.player = buildImprovementEvent.player;
            delegate.buildImprovement(buildImprovementEvent.player, buildImprovementEvent.cost, buildImprovementEvent.improvement);
        }
        
        public function result(data:Object):void
        {
            // Model updated by the remote service
            // Get the possible immediate benefits of the card (i.e. the actions that 
            // it immediately grants)
            var resultEvent:ResultEvent = ResultEvent(data);
            var actions:ListCollectionView = ArrayCollection(resultEvent.result);
            AgricolaModelLocator.getInstance().actionController.addNewActionsInQueue(actions, player);
        }
        
        public function fault(info:Object):void
        {
            var faultEvent:FaultEvent = FaultEvent(info);
            Alert.show("Could not build a major improvement: " + faultEvent.message, "Error");
        }
    
    }
}